package com.das.mechanic_base.adapter.create;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.create.ServiceInfoBean;
import com.das.mechanic_base.utils.X3GlideNewUtils;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.widget.X3ReAloneWindow;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.kproduce.roundcorners.RoundImageView;

import java.util.ArrayList;
import java.util.List;

public class ServiceInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ServiceInfoBean.WorkBaseBean> mList;

    private boolean isDetection;

    private Context mContext;

    /**
     * 0 正常
     * 1 空白占位
     */
    private int mEmptyType;

    public ServiceInfoAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
        mEmptyType = X3StringUtils.isListEmpty(mList) ? 1 : 0;
    }

    public void changeServiceList(List<ServiceInfoBean.WorkBaseBean> mList,boolean isDetection) {
        this.mList = mList;
        this.isDetection = isDetection;
        mEmptyType = X3StringUtils.isListEmpty(mList) ? 1 : 0;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new ServiceInfoHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_service_info_item,parent,false));
        }
        return new EmptyHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_empty_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == 0) {

            ServiceInfoHolder infoHolder = (ServiceInfoHolder) holder;

            infoHolder.v_line.setVisibility((position == mList.size() - 1) ? View.INVISIBLE : View.VISIBLE);

            infoHolder.itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
            infoHolder.iv_op.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (iOnClickOrder != null){
                        infoHolder.itemView.setBackgroundColor(Color.parseColor("#F7F7F7"));
                        iOnClickOrder.iOnClickReAlone(infoHolder.iv_op,1,position);
                    }
                }
            });
            String serviceBaseNameItem = mList.get(position).serviceBaseNameItem;
            if (!X3StringUtils.isEmpty(serviceBaseNameItem)) {
                List<String> serviceList = new Gson().fromJson(serviceBaseNameItem,new TypeToken<List<String>>(){}.getType());

                String serviceName = "";
                if (!X3StringUtils.isListEmpty(serviceList)) {
                    for (int i = 0; i < serviceList.size(); i++) {
                        if (i == 0) {
                            serviceName = serviceList.get(i);
                        }else {
                            serviceName = serviceName + ", " + serviceList.get(i);
                        }
                    }
                }
                infoHolder.tv_title.setText(serviceName);
            }

            long workBaseId = mList.get(position).workBaseId;
            if (workBaseId == 0) {
                infoHolder.tv_status.setText(mContext.getString(R.string.not_arrange_work));
                infoHolder.tv_status.setTextColor(ContextCompat.getColor(mContext,R.color.bg_F64444));

            }else {
                long status = mList.get(position).status;
                infoHolder.iv_op.setVisibility(View.GONE);
                switch ((int) status) {
                    case 0:
                        infoHolder.tv_status.setText(mContext.getString(R.string.x3_no_start));
                        infoHolder.tv_status.setTextColor(ContextCompat.getColor(mContext,R.color.bg_B1B3BD));
                        break;
                    case 1:
                        infoHolder.tv_status.setText(mContext.getString(R.string.x3_ongoing));
                        infoHolder.tv_status.setTextColor(ContextCompat.getColor(mContext,R.color.bg_0077FF));
                        break;
                    case 2:
                        infoHolder.tv_status.setText(mContext.getString(R.string.x3_finish));
                        infoHolder.tv_status.setTextColor(ContextCompat.getColor(mContext,R.color.c18));
                        if(isDetection){
                            infoHolder.iv_op.setVisibility(View.VISIBLE);
                        }
                        break;
                    default:
                        infoHolder.tv_status.setText(mContext.getString(R.string.x3_already_cancel));
                        infoHolder.tv_status.setTextColor(ContextCompat.getColor(mContext,R.color.bg_B1B3BD));
                        break;
                }
            }

            List<ServiceInfoBean.StaffBaseBean> staffList = mList.get(position).staffBaseList;

//            infoHolder.ll_staff_num.removeAllViews();
            if (!X3StringUtils.isListEmpty(staffList)) {
                infoHolder.ll_staff_num.setVisibility(View.VISIBLE);
                for (int i = 0; i < staffList.size(); i++) {
//                    View view = View.inflate(mContext,R.layout.x3_work_order_staff_item,null);
//                    RoundImageView rv_staff = view.findViewById(R.id.rv_staff);
//                    TextView tv_staff_name = view.findViewById(R.id.tv_staff_name);
                    X3GlideNewUtils.loadCircleImage(mContext, X3StringUtils.getImageUrl(staffList.get(i).imgUrl),infoHolder.rv_staff,R.mipmap.x3_car_owner_header);
                    infoHolder.tv_staff_name.setText(staffList.get(i).name + "");
                    infoHolder.tv_staff.setText(R.string.set_work_staff);
//                    LinearLayout.LayoutParams headLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                    headLayout.rightMargin = X3ScreenUtils.dipToPx(12,mContext);
//                    infoHolder.ll_staff_num.addView(view,headLayout);

                }
            }else {
                infoHolder.rv_staff.setImageResource(R.mipmap.x3_alone_add_tech);
                infoHolder.tv_staff.setText(R.string.x3_alone_add_person);
                infoHolder.tv_staff_name.setText("");
            }
        }else {
            EmptyHolder emptyHolder = (EmptyHolder) holder;
            emptyHolder.iv_empty.setImageResource(R.mipmap.x3_home_no_order);
            emptyHolder.tv_empty.setText(isDetection ? mContext.getString(R.string.not_have_alone_order) : mContext.getString(R.string.not_have_work_order));
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(X3ScreenUtils.dipToPx(100, mContext), X3ScreenUtils.dipToPx(100, mContext));
            layoutParams.topMargin = X3ScreenUtils.dipToPx(30, mContext);
            layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
            emptyHolder.iv_empty.setLayoutParams(layoutParams);
            LinearLayout.LayoutParams emptyLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            emptyLayout.bottomMargin = X3ScreenUtils.dipToPx(20, mContext);

        }

    }

    IOnClickOrder iOnClickOrder;

    public ServiceInfoAdapter setiOnClickOrder(IOnClickOrder iOnClickOrder) {
        this.iOnClickOrder = iOnClickOrder;
        return this;
    }

    public interface IOnClickOrder {

        void iOnClickOrder(long workBaseId,boolean isDetection);

        void iOnSaveWork(ServiceInfoBean.WorkBaseBean workBaseBean);

        void iOnAssignStaff(ServiceInfoBean.WorkBaseBean workBaseBean);

        /**
         * 跳转到报告
         * @param workBaseBean
         * @param isDetection
         */
        void iOnJumpToReport(ServiceInfoBean.WorkBaseBean workBaseBean,boolean isDetection);

        /**
         * 重新检测
         * @param workBaseId
         */
        void iOnClickReAlone(View v ,long workBaseId,int position);
    }

    @Override
    public int getItemViewType(int position) {
        return mEmptyType;
    }

    @Override
    public int getItemCount() {
        return X3StringUtils.isListEmpty(mList) ? 1 : mList.size();
    }

    class ServiceInfoHolder extends RecyclerView.ViewHolder {
        TextView tv_title;
        LinearLayout ll_staff_num;
        TextView tv_status;
        TextView tv_staff;
        RoundImageView rv_staff;
        TextView tv_staff_name;
        View v_line;
        ImageView iv_op;

        public ServiceInfoHolder(@NonNull View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            ll_staff_num = itemView.findViewById(R.id.ll_staff_num);
            tv_staff = itemView.findViewById(R.id.tv_staff);
            tv_status = itemView.findViewById(R.id.tv_status);
            iv_op = itemView.findViewById(R.id.iv_op);
            v_line = itemView.findViewById(R.id.v_line);
            tv_staff = itemView.findViewById(R.id.tv_staff);
            rv_staff = itemView.findViewById(R.id.rv_staff);
            tv_staff_name = itemView.findViewById(R.id.tv_staff_name);

            itemView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            itemView.setBackgroundColor(Color.parseColor("#F7F7F7"));
                            break;
                        case MotionEvent.ACTION_UP:
                        case MotionEvent.ACTION_CANCEL:
                            itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));

                            break;


                    }
                    return false;
                }
            });

            itemView.setOnClickListener(view -> {
                ServiceInfoBean.WorkBaseBean workBaseBean = mList.get(getAbsoluteAdapterPosition());
                if (workBaseBean.status == 3) {
                    // 取消不能点击
                    return;
                }
                if (workBaseBean.workBaseId == 0) {
                    // 未安排施工
                    if (iOnClickOrder != null) {
                        iOnClickOrder.iOnSaveWork(workBaseBean);
                    }
                    return;
                }
                if (workBaseBean.status == 2) {
                    if (iOnClickOrder != null) {
                        iOnClickOrder.iOnJumpToReport(mList.get(getAbsoluteAdapterPosition()),isDetection);
                    }
                    return;
                }
                if (iOnClickOrder != null) {
                    iOnClickOrder.iOnClickOrder(workBaseBean.workBaseId,isDetection);
                }
            });
            ll_staff_num.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            itemView.setBackgroundColor(Color.parseColor("#F7F7F7"));
                            break;
                        case MotionEvent.ACTION_UP:
                        case MotionEvent.ACTION_CANCEL:
                            itemView.setBackgroundColor(Color.parseColor("#FFFFFF"));
                            break;
                    }
                    return false;
                }
            });
            ll_staff_num.setOnClickListener(v -> {
                ServiceInfoBean.WorkBaseBean workBaseBean = mList.get(getAbsoluteAdapterPosition());
                long status = workBaseBean.status;
                if (status == 0 || status == 1) {
                    // 覆盖安排施工人员
                    if (iOnClickOrder != null) {
                        iOnClickOrder.iOnAssignStaff(workBaseBean);
                    }
                }
            });
        }
    }

    class EmptyHolder extends RecyclerView.ViewHolder {
        TextView tv_empty;
        ImageView iv_empty;
        public EmptyHolder(@NonNull View itemView) {
            super(itemView);
            tv_empty = itemView.findViewById(R.id.tv_empty);
            iv_empty = itemView.findViewById(R.id.iv_empty);
        }
    }
}
